import React, { useState, useEffect } from 'react';
import { TextareaItem, Button, Toast } from 'antd-mobile';
import SelfHead from 'components/common/selfHead';
import { addFullBack, findFullBackDetail } from 'ser/actions';
import { history } from 'umi';

import './index.less';
// 稳产满产反馈
export default function Detail({
  location: {
    query: { feedbackId = '' },
  },
}) {
  const [detail, setDetail] = useState({});
  const [flag, setFlag] = useState(false);
  useEffect(() => {
    if (feedbackId) {
      async function getData() {
        const res = await findFullBackDetail(feedbackId);
        if (res.code === 0) {
          setDetail(res.rows);
        }
      }
      getData();
    }
  }, []);
  const [appeal, setAppeal] = useState('');
  const [sugg, setSugg] = useState('');
  async function submitData() {
    if (flag) {
      Toast.info('不能重复添加', 1);
      return;
    }
    if (!appeal && !sugg) {
      Toast.info('请将信息填写完整', 1);
    } else {
      setFlag(true);
      const res = await addFullBack({
        knysq: appeal,
        jyyyj: sugg,
        status: 1,
      });
      if (res.code === 0) {
        Toast.info(res.msg, 1);
        history.push('/fullBack');
      } else {
        setFlag(false);
      }
    }
  }
  return (
    <div className="detail">
      <SelfHead title={'稳产满产反馈'} />
      <div className="content">
        <span>困难与诉求</span>
        {detail.knysq ? (
          <TextareaItem value={detail.knysq} disabled={feedbackId} />
        ) : (
          <TextareaItem onChange={v => setAppeal(v)} disabled={feedbackId} />
        )}
        <span>建议和意见</span>
        {detail.jyyyj ? (
          <TextareaItem value={detail.jyyyj} disabled={feedbackId} />
        ) : (
          <TextareaItem onChange={v => setSugg(v)} disabled={feedbackId} />
        )}
        {feedbackId ? null : (
          <Button type="primary" onClick={submitData}>
            提交
          </Button>
        )}
      </div>
    </div>
  );
}
